# -*- coding: utf-8 -*-
"""
Created on Sat Sep  7 15:58:18 2024

@author: Hasee
"""

from plane_sprites import *
import pygame
import time

class PlaneGame():

    
    def __init__(self):
        print('游戏初始化')
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)
        self.clock = pygame.time.Clock()
        self.__create__sprites()
        #定时器事件
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
        
        self.__proTime = 0
        self.__endTime = 0
        self.__flag = 0
        
    #添加精灵组
    def __create__sprites(self):
        
        bg1 = Background()
        bg2 = Background(True)
        self.back_group = pygame.sprite.Group(bg1,bg2)
        
        self.enemy_group = pygame.sprite.Group()
        
        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)
        
    
    def start_game(self):
        print("游戏开始")
        
        filepath="./567665407.mp3"
        pygame.mixer.init()
        pygame.mixer.music.load(filepath)
        pygame.mixer.music.play()
        while True:
            self.clock.tick(FRAME)
            self.__event__handler()
            self.__check_collide()
            self.__update_sprites()
            pygame.display.update()
        
    def __event__handler(self):
        eventList = pygame.event.get()
        for event in eventList:
            if event.type == pygame.QUIT:
                PlaneGame.__game_over()
            elif event.type == CREATE_ENEMY_EVENT:
                 #  print("敌机出场")
                  enemy = Enemy()
                  self.enemy_group.add(enemy)
            
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_a]:
            self.hero.speed=-V
            self.__flag = 1
        elif keys_pressed[pygame.K_d]:
            self.hero.speed=V
            self.__flag = 1
        elif keys_pressed[pygame.K_w]:
            self.hero.speed=-V
            self.__flag = 2
        elif keys_pressed[pygame.K_s]:
            self.hero.speed=V
            self.__flag = 2
        else:
            self.hero.speed=0
            if keys_pressed[pygame.K_j]:
                #发射子弹
                #冷却时间0.5秒
                self.__endTime = pygame.time.get_ticks()
                if self.__endTime-self.__proTime>=500:
                    self.__proTime = self.__endTime
                    self.hero.fire()

        
    @staticmethod
    def __game_over():
        print("游戏结束")
        pygame.quit()
        exit()
        
    def __check_collide(self):
        #第三个参数为真销毁第一个参数，第四个参数为真销毁第二个参数
        pygame.sprite.groupcollide(self.hero.bullet_group, self.enemy_group, True, True)
        
        enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
        
        if len(enemies) :
            self.hero.kill()
            PlaneGame.__game_over()
        
        
    def __update_sprites(self):
        self.back_group.update()
        self.back_group.draw(self.screen)
        self.enemy_group.update()
        self.enemy_group.draw(self.screen)
        
        self.hero_group.update(self.__flag)
        self.hero_group.draw(self.screen)
        
        self.hero.bullet_group.update()
        self.hero.bullet_group.draw(self.screen)
    

if __name__ == "__main__":
    game = PlaneGame()
    game.start_game()        